//
//  PicturesView.m
//  知乎日报
//
//  Created by 陈明光 on 2019/9/5.
//  Copyright © 2019 陈明光. All rights reserved.
//
//封装轮播图
#import "PicturesView.h"
#import "PicturesModel.h"
#import "PicturesimageView.h"
@interface PicturesView()<UIScrollViewDelegate>
//页码
@property(nonatomic,strong)UIScrollView *scrollView;
//小点点
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSMutableArray *imagesViews;
//设置一个定时器
@property(nonatomic,strong)NSTimer *timer;
@end
@implementation PicturesView
//重写init方法
-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        UIScrollView *scrollView=[[UIScrollView alloc]init];
        [self addSubview:scrollView];
        scrollView.pagingEnabled=YES;
        scrollView.bounces=NO;
        scrollView.delegate=self;
        scrollView.showsHorizontalScrollIndicator=NO;
        self.scrollView=scrollView;
        
        UIPageControl *page=[[UIPageControl alloc]init];
        self.pageControl=page;
        [self addSubview:page];
        
        //布局
        scrollView.sd_layout.topSpaceToView(self, 0).leftSpaceToView(self, 0).rightSpaceToView(self, 0).bottomSpaceToView(self, 0);
        //绑定小点点的坐标
        page.sd_layout.centerXEqualToView(self).widthIs(60).heightIs(15).bottomSpaceToView(self, 10);
        
    }
    return self;
}
-(void)setImages:(NSArray *)images{
    _images=images;
    self.pageControl.numberOfPages=images.count;
    
    for (PicturesModel *model in images) {
        PicturesimageView *imageView=[[PicturesimageView alloc]init];
        imageView.model=model;
        //将图片放到数组和视图里面
        
        [self.scrollView addSubview:imageView];
        
        //******布局，轮播图imageView具体的位置******
        if (self.imagesViews.count==0) {
            imageView.sd_layout.topSpaceToView(self.scrollView, 0).leftSpaceToView(self.scrollView, 0).bottomSpaceToView(self.scrollView, 0).widthRatioToView(self.scrollView,1);
        }else{
            imageView.sd_layout.topSpaceToView(self.scrollView, 0).leftSpaceToView([self.imagesViews lastObject], 0).bottomSpaceToView(self.scrollView, 0).widthRatioToView(self.scrollView,1);
        }
        [self.imagesViews addObject:imageView];
        //设置滚动
        self.scrollView.contentSize=CGSizeMake(self.imagesViews.count*self.width, self.height);
        
    }
    //滚动完后要调用
    [self addTimer];
}
//用户滚动结束之后调用，改变小点点的坐标
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat xOffset=scrollView.contentOffset.x;
  //  NSLog(@"%f",xOffset);
    //0不能为被除数
    int currenPage=(int)(xOffset/self.width+0.5);
    //把坐标添加到视图中
    self.pageControl.currentPage=currenPage;
}
//当用户开始滚动的时候要停止addTimer方法
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self removeTimer];
}
//当用户滚动结束的时候要继续调用addTimer方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [self addTimer];
}
//自动滚动轮播图的方法
-(void)addTimer{
    //每隔5秒调用下一张图片(nexImage)的方法
    self.timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(nexImage) userInfo:nil repeats:YES];
}
//当用户开始滚动的时候要停止addTimer方法
-(void)removeTimer{
    [self.timer invalidate];
}
-(void)nexImage{
    NSInteger page=(self.pageControl.currentPage +1)%self.pageControl.numberOfPages;
    CGFloat xOffset=page*self.width;
    [self.scrollView setContentOffset:CGPointMake(xOffset, 0) animated:YES];
}
-(NSMutableArray *)imagesViews{
    if (!_imagesViews) {
        _imagesViews=[NSMutableArray new];
    }
    return _imagesViews;
}


@end
